package com.wrl.mmall.util;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;


/**
 * 使用joda.time开源工具类做时间处理
 * string转date
 * date转string
 * 针对时间的day，做加减
 * 针对时间的minute，做加减（参数返回值都是date）
 * 各种判断时间是不是在指定范围内
 *
 * @author Wang Renlei
 * @Date 2019/6/4
 */
public class DateTimeUtil {

    public static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String HHmmss = "HH:mm:ss";

    public static long startTimeAm;
    public static long endTimeAm;
    public static long startTimePm;
    public static long endTimePm;

    static {
        startTimeAm = strToDate("09:30:00", HHmmss).getTime();
        endTimeAm = strToDate("11:30:00", HHmmss).getTime();
        startTimePm = strToDate("13:00:00", HHmmss).getTime();
        endTimePm = strToDate("15:00:00", HHmmss).getTime();
    }

    // String --> date
    public static Date strToDate(String dateTimeStr, String formatStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    //  date --> String
    public static String dateToStr(Date date, String formatStr) {
        if (date == null) {
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }

    // String --> date
    public static Date strToDate(String dateTimeStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(yyyyMMddHHmmss);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    //  date --> String
    public static String dateToStr(Date date) {
        if (date == null) {
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(yyyyMMddHHmmss);
    }


    /**
     * 天数加减
     * 参数和返回值都是String类型
     */
    public static String dateSubDayString(String date, int i) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(yyyyMMddHHmmss);
        DateTime dateTime = dateTimeFormatter.parseDateTime(date);
        return dateTime.plusDays(i).toString(yyyyMMddHHmmss);
    }

    /**
     * 天数加减
     * 参数和返回值都是date类型
     */
    public static Date dateSubDayDate(Date date, int i) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusDays(i).toDate();
    }

    /**
     * 加减分钟
     * 参数和返回值都是date类型
     */
    public static Date dateSubMinute(Date date, int i) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMinutes(i).toDate();
    }

    /**
     * 加减分钟（强化版）
     * 只对开盘时间进行加减，跳过周末，跳过节假日
     * 例：13:01:00 减一分钟是 11:30:00
     * 例：09:31:00 减一分钟是 15:00:00
     */
    public static Date dateSubMinWordDay(Date date, int count) {
        date = updateTimeSecond(date);
        int n = 1;
        n = count > 0 ? n : -n;
        for (int i = 0; i < (count > 0 ? count : -count); ) {
            //是周末，跳过
            if (isWeekend(date)) {
                date = dateSubDayDate(date, -1);
                continue;
            }
            date = dateSubMinute(date, n);
            if (isCalcTime(date)) {
                i++;
            }
        }
        return updateTimeSecond(date);
    }



    /**
     * 修正时间，秒级别
     * 把参数中时间的 秒，改为00
     */
    public static Date updateTimeSecond(Date date) {
        String s = dateToStr(date, "yyyy-MM-dd HH:mm:00");
        return strToDate(s);
    }

    /**
     * 修正时间，分钟级别
     * 把参数中时间的 分、秒，改为00
     */
    public static Date updateTimeMinute(Date date) {
        String s = dateToStr(date, "yyyy-MM-dd HH:00:00");
        return strToDate(s);
    }

    /**
     * 修正时间，小时级别
     * 把参数中时间的 时、分、秒，改为00
     */
    public static Date updateTimeHour(Date date) {
        String s = dateToStr(date, "yyyy-MM-dd 00:00:00");
        return strToDate(s);
    }

    /**
     * 判断是否为周六或周日
     * 是：true
     * 否：false
     */
    public static boolean isWeekend(Date date) {
        DateTime dateTime = new DateTime(date);
        if (dateTime.getDayOfWeek() == 6 || dateTime.getDayOfWeek() == 7) {
            return true;
        }
        return false;
    }

    /**
     * 判断是不是开盘时间
     * 09:30 - 11:30
     * 13:00 - 15:00
     */
    public static boolean isWorkTime(Date date) {
        String s = dateToStr(date, HHmmss);
        long temp = strToDate(s, HHmmss).getTime();
        return temp >= startTimeAm && temp <= endTimeAm || temp >= startTimePm && temp <= endTimePm;
    }

    /**
     * 判断时间在以下范围内
     * 09:31 - 11:30
     * 13:01 - 15:00
     */
    public static boolean isCalcTime(Date date) {
        String s = dateToStr(date, HHmmss);
        long temp = strToDate(s, HHmmss).getTime();
        return temp > startTimeAm && temp <= endTimeAm || temp > startTimePm && temp <= endTimePm;
    }

    /**
     * 判断是不是 09:31
     */
    public static boolean isCalcStartTime(Date date) {
        String s = dateToStr(date, HHmmss);
        long temp = strToDate(s, HHmmss).getTime();
        if (temp == startTimeAm) {
            return true;
        }
        return false;
    }

    /**
     * 比较两个时间是否相等
     * 精确到秒
     */
    public static boolean compareTime(Date d1, Date d2) {
        if (StringUtils.equals(dateToStr(d1), dateToStr(d2))) {
            return true;
        }
        return false;
    }


}
